package org.jground.rx.ui.zk;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.rx.IRXObject;
import org.jground.rx.RXClass;
import org.jground.rx.RXSystem;
import org.jground.rx.ui.IMainPanel;
import org.jground.zk.zulX.WindowX;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Caption;

public class UIViewWindow extends WindowX implements IMainPanel<Component> {

    private static Log logger = LogFactory.getLog(UIViewWindow.class);
    //

    protected UIFrame uiFrame;
    //
    protected RXClass moClass;
    private Caption caption;
    protected UIDataObjectInfoBar infoBar;
    protected IRXObject currentRxd;

    protected UIViewPanel viewPanel;
    private UIWindowViewCommandPanel viewCommandPanel;

    public UIViewWindow(UIFrame uiFrame, String moid) {
        logger.debug("create UIViewWindow");
        this.uiFrame = uiFrame;
        this.moClass = RXSystem.getInstance().getRxService().getRXClass(moid);
        this.setBorder("normal");
        //
        // COMPONENTS
        //
        caption = new Caption();
        //
        infoBar = new UIDataObjectInfoBar();
        infoBar.setSclass(UIStyles.BAR);

        //
        viewPanel = new UIViewPanel(uiFrame, moClass.getMoid());
        viewCommandPanel = new UIWindowViewCommandPanel(uiFrame, this);
        //
        // LAYOUT
        //
        // this.add(title);
        this.add(caption);
        this.add(infoBar);
        this.add(viewPanel.getView());
        this.add(viewCommandPanel);
        this.setClosable(true);
        this.addEventListener(Events.ON_CLOSE, viewCommandPanel);
    }

    public void beforeShow() {}

    public void beforeClose() {
        uiFrame.getPrefService().addViewed(currentRxd);
        uiFrame.getPrefService().setFocusDataView(currentRxd, viewPanel.getFocusData());
    }

    public Component getView() {
        return this;
    }

    //
    // Data Methods
    //

    public void setData(IRXObject rxd) {
        currentRxd = rxd;
        clearData();
        if (rxd != null) {
            // String imageUri = UIStyles.getInstance().getImageUri(mod,
            // UIStyles.S);
            //            
            // caption.setImage(imageUri);
            caption.setImageContent(UIStyles.getInstance().getImage(rxd, UIStyles.S));
            this.setTitle(" " + moClass.getName() + " : " + uiFrame.getUISynopsisRendererFactory().toString(rxd));
            infoBar.setData(rxd);
            //
            viewPanel.setData(rxd);
            viewCommandPanel.setData(rxd);
            //     
            viewPanel.setFocusData(uiFrame.getPrefService().getFocusDataView(currentRxd));
        }
    }

    void clearData() {
        viewPanel.clearData();
    }

    public void close() {
        if (currentRxd != null) {
            uiFrame.close(this.currentRxd.getOid());
        }
    }

    // public FocusData getFocusData() {
    // return viewPanel.getFocusData();
    // }

    // public void setFocusData(FocusData fd) {
    // viewPanel.setFocusData(fd);
    // }

}